package com.dahua.ui.views;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;

import com.dahua.R;
import com.dahua.ui.base.BaseActivity;
import com.dahua.utils.ObjUtil;


public class LoadingDialog extends Dialog {
	private View mPanel;
	private TextView messageTextView;
	private Context context;

	public LoadingDialog(Context context) {
		super(context, R.style.dialog_loading_style);
		this.context = context;
		mPanel = getLayoutInflater().inflate(R.layout.dialog_loading, null);

		messageTextView = (TextView) mPanel.findViewById(R.id.loading_message);
		int width=0;
		if(context instanceof BaseActivity){
			 width = ((BaseActivity) context).getScreenWith() / 4;
		}
		mPanel.setMinimumHeight(width);
		mPanel.setMinimumWidth(width);

		setContentView(mPanel);
	}

	public void setMessage(CharSequence message) {
		int visible = View.VISIBLE;
		if (ObjUtil.isEmpty(message)) {
			visible = View.GONE;
		}
		if (messageTextView.getVisibility() != visible) {
			messageTextView.setVisibility(visible);
		}
		messageTextView.setText(message);
	}

	public static Builder create(Context context) {
		return new Builder(context);
	}

	public static class Builder {

		private Context mContext;
		private CharSequence message;
		private boolean cancelaOnTouchOutside;
		private boolean cancelable;
		private boolean mFinishWhenDismiss;
		private OnCancelListener mCancelListener;

		public Builder(Context context) {
			mContext = context;
			cancelable = true;
			cancelaOnTouchOutside = false;
		}

		public Builder setMessage(CharSequence message) {
			this.message = message;
			return this;
		}

		public Builder setCanceledOnTouchOutside(boolean cancelable) {
			this.cancelaOnTouchOutside = cancelable;
			return this;
		}

		public Builder setCancelable(boolean cancelable) {
			this.cancelable = cancelable;
			return this;
		}

		public Builder setOnCancelListener(OnCancelListener listener) {
			this.mCancelListener = listener;
			return this;
		}

		public Builder setFinishWhenDismiss(boolean finish) {
			this.mFinishWhenDismiss = finish;
			return this;
		}

		public LoadingDialog show() {
			LoadingDialog dialog = new LoadingDialog(mContext);
			dialog.setMessage(message);
			dialog.setCanceledOnTouchOutside(cancelaOnTouchOutside);
			dialog.setCancelable(cancelable);
			dialog.setOnCancelListener(mCancelListener);
			dialog.show();
			if (mFinishWhenDismiss) {
				dialog.setOnDismissListener(new OnDismissListener() {
					@Override
					public void onDismiss(DialogInterface dialogInterface) {
						if (mContext != null
								&& ((mContext instanceof Activity) && !((Activity) mContext).isFinishing())) {
							((Activity) mContext).finish();
						}
					}
				});
			}
			return dialog;
		}
	}

	@Override
	public void show() {
		if (!isInvaid(context) && !isShowing()) {
			super.show();
		}
	}

	@Override
	public void dismiss() {
		if (!isInvaid(context)) {
			super.dismiss();
		}
	}

	public static boolean isInvaid(Context context) {
		return context == null || ((context instanceof Activity) && ((Activity) context).isFinishing());
	}
}
